package com.hzqx.syndata.falsedemo.service;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import com.hzqx.syndata.result1.ResultBuilder;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Slf4j
@Service
public class TestCacheService {

    @Cacheable(cacheNames = "flm",key = "#root.methodName+#code",sync=true)
    public String getData(String code){
        RestTemplate restTemplate = new RestTemplate();
        String url ="http://172.18.110.83:9095/flm/system/object/findFlmCodeNum?areaCode="+code;
        String num = restTemplate.getForObject(url,String.class);
        //log.info(num);
        return num;
    }

    @Cacheable(cacheNames = "flm",key = "#root.methodName+#areaCode+#codeColor",sync=true)
    public String getDeatail(String areaCode, String codeColor){
        RestTemplate restTemplate = new RestTemplate();
        String url ="http://172.18.110.83:9095/flm/system/object/findCompanyDetail?areaCode="+areaCode+"&codeColor="+codeColor;
        String result = restTemplate.getForObject(url,String.class);
        return result;
    }

    @CacheEvict(cacheNames = "flm",allEntries = true)
    public void getDataCompany(){
        log.info("delete all flm cache");
    }

    public Integer getww()  {
        int a = 1/0;
        return a;
    }

    @Cacheable(cacheNames = "teste",key = "#root.methodName+#code",sync=true)
    public String testExcep(String code){
        String abc = "dfsddgdfg---";
        double a = Math.random()*10;
        if(a>5){
            System.out.println("a的值为"+a);
            a=a/0;
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return abc+a;
    }


    @CacheEvict(cacheNames = "teste",allEntries = true)
    public void delete(){
        log.info("delete all test cache");
    }
}
